package com.dingwen.treasure.model.entity.mybatisplus;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.dingwen.treasure.base.enums.RuleEnum;
import com.dingwen.treasure.base.enums.StatusEnum;
import com.dingwen.treasure.base.util.ConvertUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * JS校验规则业务案例
 *
 * @author dingwen
 * @TableName business_rule
 */
@ApiModel(value = "BusinessRule", description = "业务规则")
@TableName(value = "business_rule")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class BusinessRule extends BaseEntity implements Serializable {

    @TableField(exist = false)
    private static final long serialVersionUID = -4524746075035423471L;
    /**
     * 主键
     */
    @TableId(value = "rule_id", type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "主键")
    private Long ruleId;

    /**
     * 校验规则名称
     */
    @TableField(value = "rule_name")
    @ApiModelProperty(value = "校验规则名称")
    private String ruleName;

    /**
     * 规则描述
     */
    @TableField(value = "rule_description")
    @ApiModelProperty(value = "规则描述")
    private String ruleDescription;

    /**
     * 规则状态：0禁用 1启用
     */
    @TableField(value = "rule_state")
    @ApiModelProperty(value = "规则状态：0禁用 1启用")
    private StatusEnum ruleState;

    /**
     * 规则内容
     */
    @TableField(value = "rule_content")
    @ApiModelProperty(value = "规则内容")
    private String ruleContent;

    /**
     * 校验字段名称(所需要多个字段逗号分隔)
     */
    @TableField(value = "field_name")
    @ApiModelProperty(value = " 校验字段名称(所需要多个字段逗号分隔)")
    private String fieldName;

    /**
     * 规则Code
     */
    @TableField(value = "rule_code")
    @ApiModelProperty(value = "规则Code")
    private String ruleCode;

    /**
     * 规则类型 {@link RuleEnum}
     */
    @TableField(value = "rule_type")
    @ApiModelProperty(value = "规则类型：0必填 1长度 2必填+长度 3敏感词 4正则")
    private RuleEnum ruleType;

    /**
     * 业务Id
     */
    @TableField(value = "business_id")
    @ApiModelProperty(value = "业务Id")
    private Long businessId;

    /**
     * 获取所有字段名称 （包括当前校验字段以及当前校验字段所需值字段名称）
     *
     * @return {@link List}<{@link String}>
     */
    public List<String> getFieldNames() {
        if (StrUtil.isEmpty(this.fieldName)) {
            return null;
        }
        return ConvertUtil.strToList(this.fieldName);
    }

    /**
     * 得到当前进行校验的字段名称
     *
     * @return {@link String}
     */
    public String getCurrentFieldName() {
        List<String> fieldNames = getFieldNames();
        return CollUtil.isEmpty(fieldNames) ? null : fieldNames.get(0);
    }


}